package dabs.engine;

public class Valve extends ComponentPart implements AcceptsModifier{
	private boolean isOpen;
	private boolean valveFault;
	private int timeToFault;
	private int timeLeftFault;
	
	public Valve(PowerPlant plant, String name, boolean openState) {
		super(plant, name);
		isOpen = openState;
	}

	public boolean getIsOpen() { return isOpen; }
	
	public void setIsOpen(boolean isOpen){ 
		if (valveFault == false){ this.isOpen = isOpen; }
		else { 
			super.passMessage(new Notification(super.getName(), 
					"Valve is blocked. Will open in " + timeLeftFault + " seconds!"));
		}
	}
	
	public void handleModifier(Modifier modifier) {
		if (modifier instanceof TemporaryDisable){
			super.passMessage(new Notification(super.getName(), 
					"Valve will be blocked in 30 seconds!"));
			timeToFault = 30;
			timeLeftFault = modifier.getDuration();
		}
	}
	
	public void tick(){
		if (timeToFault == 1){
			timeToFault--;
			valveFault = true;
			isOpen = false;
			super.passMessage(new Notification(super.getName(),
					"Valve blocked for " + timeLeftFault + " seconds!"));
		}
		else if (timeToFault > 1) { timeToFault--; }
		else if (valveFault == false) { super.tick(); }
		else {
			if (timeLeftFault == 0){
				super.passMessage(new Notification(super.getName(), 
						"Valve unblocked. Defaulting to closed position."));
				valveFault = false;
				isOpen = false;
				}
			else { timeLeftFault--; }
		}
	}
}
